package com.boot.mall.third.service.impl;

import com.boot.mall.third.service.EmailSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service
public class EmailSendServiceImpl implements EmailSendService {

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public boolean sendEmail(String code,String email) {
        //创建消息对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper;

        String from = "2565036109@qq.com";
        String content = "尊敬的用户，您好:<br><h3 style=\"color: red\">您的谷粒商城注册验证码为  <h2>"+code+"</h2></h3>" +
                "<br><h3>验证码五分钟内有效</h3>";
        String subject = "【谷粒商城注册验证码】";

        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);    //发送方
            mimeMessageHelper.setTo(email); //邮件地址
            mimeMessageHelper.setText(content,true);    //内容
            mimeMessage.setSubject(subject);    //主题

            mailSender.send(mimeMessage);   //发送
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
